package vashion.azeroth.api.controller.member;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import vashion.azeroth.api.controller.base.BaseController;
import vashion.azeroth.back.core.context.BackUserContext;
import vashion.azeroth.contant.GenericErrorEnum;
import vashion.azeroth.contant.PublishFilesErrorEnum;
import vashion.azeroth.core.personal.pojo.PersonalFeed;
import vashion.azeroth.core.personal.pojo.PersonalPublishFiles;
import vashion.azeroth.core.result.ApiResult;
import vashion.azeroth.core.service.PersonalPublishFilesService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/member")
public class PublishFilesController<T> extends BaseController {

    private final static Logger log = LoggerFactory
            .getLogger(AddressController.class);

    @Autowired
    private PersonalPublishFilesService<T> personalPublishFilesService;

    @Autowired
    private BackUserContext backUserContext;

    /**
     *
     * @param publishFiles
     * @return
     */
    @RequestMapping("publishFeed.htm")
    @ResponseBody
    public ApiResult<ArrayList<PersonalPublishFiles>> publishFile(
            @ModelAttribute PersonalPublishFiles publishFiles) {

        ApiResult<ArrayList<PersonalPublishFiles>> result = new ApiResult<ArrayList<PersonalPublishFiles>>();
        try {
            if (null == publishFiles) {
                return ApiResult.newErrorResult(
                        GenericErrorEnum.NOT_EMPTY.getErrorCode(), "参数"
                                + GenericErrorEnum.NOT_EMPTY.getErrorMessage());
            }

            if (null == publishFiles.getMemberId()
                    || StringUtils.isBlank(String.valueOf(publishFiles
                    .getMemberId()))) {
                return ApiResult.newErrorResult(
                        GenericErrorEnum.NOT_EMPTY.getErrorCode(), "会员ID"
                                + GenericErrorEnum.NOT_EMPTY.getErrorMessage());
            }

            if (StringUtils.isBlank(publishFiles.getContent())) {
                return ApiResult.newErrorResult(
                        GenericErrorEnum.NOT_EMPTY.getErrorCode(), "文章内容"
                                + GenericErrorEnum.NOT_EMPTY.getErrorMessage());
            }

            if (StringUtils.isBlank(publishFiles.getFeedPic())) {
                return ApiResult.newErrorResult(
                        GenericErrorEnum.NOT_EMPTY.getErrorCode(), "帖子图片地址"
                                + GenericErrorEnum.NOT_EMPTY.getErrorMessage());
            }

            result = personalPublishFilesService.addFile(publishFiles);
        } catch (Exception e) {
            log.error("发布文章异常", e);
            return ApiResult.newErrorResult(
                    PublishFilesErrorEnum.PUB_FILE_FAIL.getErrorCode(),
                    PublishFilesErrorEnum.PUB_FILE_FAIL.getErrorMessage());
        }
        return result;
    }

    @RequestMapping("getFeeds.htm")
    @ResponseBody
    public ApiResult<ArrayList<PersonalFeed>> getFeeds(PersonalPublishFiles files) {

        ApiResult<ArrayList<PersonalFeed>> result = new ApiResult<ArrayList<PersonalFeed>>();
        try {
            if (null == files) {
                return ApiResult.newErrorResult(
                        GenericErrorEnum.NOT_EMPTY.getErrorCode(), "参数"
                                + GenericErrorEnum.NOT_EMPTY.getErrorMessage());
            }
            
            if (null == files.getSortFlag()) {
				files.setSortFlag(1);
			}



            if (files.getSortFlag() == 0) { //按精华获取帖子
                totalCounts = personalPublishFilesService.findCntChosen(files);
                initPages(files, totalCounts);
                result = personalPublishFilesService.getFilesByChosen(files, totalCounts);
            }

            if (files.getSortFlag() == 1) { //按最新获取帖子
                totalCounts = personalPublishFilesService.findCntTimeStamp(files);
                initPages(files, totalCounts);
                result = personalPublishFilesService.getFilesByTimeStamp(files, totalCounts);
            }

            if (files.getSortFlag() == 2) { //按关注的人获取帖子
            	
                if (null == files.getMemberId()) {
                  initPages(files, totalCounts);
                  List<PersonalPublishFiles> list = personalPublishFilesService.find(files);
              }
                
                totalCounts = personalPublishFilesService.cntFilesByMyFollowUser(files);
                initPages(files, totalCounts);
                result = personalPublishFilesService.getFilesByFollower(files, totalCounts);
            }

            if (files.getSortFlag() == 3) { //按tag获取帖子
                totalCounts = personalPublishFilesService.findCntTags(files);
                result = personalPublishFilesService.getFilesByTags(files, totalCounts);
            }

            if (files.getSortFlag() == 4) { //按memberId获取帖子(我的发布)
                if (null == files.getMemberId()) {
                  initPages(files, totalCounts);
                  List<PersonalPublishFiles> list = personalPublishFilesService.find(files);

              }
                
                totalCounts = personalPublishFilesService.findCntMembers(files);
                initPages(files, totalCounts);
                result = personalPublishFilesService.getFilesBymemberId(files, totalCounts);
            }

        } catch (Exception e) {
            log.error("获取帖子异常", e);
            return ApiResult.newErrorResult(
                    PublishFilesErrorEnum.GET_FILES_FAIL.getErrorCode(),
                    PublishFilesErrorEnum.GET_FILES_FAIL.getErrorMessage());
        }
        return result;
    }

    @RequestMapping("getPersonalFileList.htm")
    @ResponseBody
    public ApiResult<List<PersonalPublishFiles>> getPersonalFileList(PersonalPublishFiles files) {

        try {
            files.setPosFlag(1);
            List<PersonalPublishFiles> list = personalPublishFilesService.find(files);
            return ApiResult.newSuccessResult(list);

        } catch (Exception e) {
            log.error("获取帖子异常", e);
            return ApiResult.newErrorResult(
                    PublishFilesErrorEnum.GET_FILES_FAIL.getErrorCode(),
                    PublishFilesErrorEnum.GET_FILES_FAIL.getErrorMessage());
        }
    }

    @RequestMapping("getFilesByTags.htm")
    @ResponseBody
    public ApiResult<ArrayList<PersonalFeed>> getFilesByTags(PersonalPublishFiles files) {

        ApiResult<ArrayList<PersonalFeed>> result = new ApiResult<ArrayList<PersonalFeed>>();
        try {
            if (null == files) {
                return ApiResult.newErrorResult(
                        GenericErrorEnum.NOT_EMPTY.getErrorCode(), "参数"
                                + GenericErrorEnum.NOT_EMPTY.getErrorMessage());
            }

            if (null == files.getTags()) {
                return ApiResult.newErrorResult(
                        GenericErrorEnum.NOT_EMPTY.getErrorCode(), "标签"
                                + GenericErrorEnum.NOT_EMPTY.getErrorMessage());
            }

            Integer totalCounts = personalPublishFilesService.findCntTags(files);
            initPages(files, totalCounts);
            result = personalPublishFilesService.getFilesByTags(files, totalCounts);
        } catch (Exception e) {
            log.error("获取精选帖子异常", e);
            return ApiResult.newErrorResult(
                    PublishFilesErrorEnum.GET_FILES_FAIL.getErrorCode(),
                    PublishFilesErrorEnum.GET_FILES_FAIL.getErrorMessage());
        }
        return result;
    }

    @RequestMapping("getFilesByChosen.htm")
    @ResponseBody
    public ApiResult<ArrayList<PersonalFeed>> getFilesByChosen(
            PersonalPublishFiles files) {

        ApiResult<ArrayList<PersonalFeed>> result = new ApiResult<ArrayList<PersonalFeed>>();
        try {
            if (null == files) {
                return ApiResult.newErrorResult(
                        GenericErrorEnum.NOT_EMPTY.getErrorCode(), "参数"
                                + GenericErrorEnum.NOT_EMPTY.getErrorMessage());
            }

            Integer totalCounts = personalPublishFilesService.findCntChosen(files);
            initPages(files, totalCounts);
            result = personalPublishFilesService.getFilesByChosen(files, totalCounts);
        } catch (Exception e) {
            log.error("获取精选帖子异常", e);
            return ApiResult.newErrorResult(
                    PublishFilesErrorEnum.GET_FILES_FAIL.getErrorCode(),
                    PublishFilesErrorEnum.GET_FILES_FAIL.getErrorMessage());
        }
        return result;
    }

    @RequestMapping("getFilesByTimeStamp.htm")
    @ResponseBody
    public ApiResult<ArrayList<PersonalFeed>> getFilesByTimeStamp(
            PersonalPublishFiles files) {

        ApiResult<ArrayList<PersonalFeed>> result = new ApiResult<ArrayList<PersonalFeed>>();
        try {
            if (null == files) {
                return ApiResult.newErrorResult(
                        GenericErrorEnum.NOT_EMPTY.getErrorCode(), "参数"
                                + GenericErrorEnum.NOT_EMPTY.getErrorMessage());
            }

            Integer totalCounts = personalPublishFilesService.findCntTimeStamp(files);
            initPages(files, totalCounts);
            result = personalPublishFilesService.getFilesByTimeStamp(files, totalCounts);
        } catch (Exception e) {
            log.error("获取最新帖子异常", e);
            return ApiResult.newErrorResult(
                    PublishFilesErrorEnum.GET_FILES_FAIL.getErrorCode(),
                    PublishFilesErrorEnum.GET_FILES_FAIL.getErrorMessage());
        }
        return result;
    }

    @RequestMapping("getFilesByFollower.htm")
    @ResponseBody
    public ApiResult<ArrayList<PersonalFeed>> getFilesByFollower(
            PersonalPublishFiles files) {

        ApiResult<ArrayList<PersonalFeed>> result = new ApiResult<ArrayList<PersonalFeed>>();
        try {
            if (null == files) {
                return ApiResult.newErrorResult(
                        GenericErrorEnum.NOT_EMPTY.getErrorCode(), "参数"
                                + GenericErrorEnum.NOT_EMPTY.getErrorMessage());
            }

//			Integer totalCounts = personalPublishFilesService.findCntFollower(files);//这个数量获取不到，分页功能在service里实现
//			initPages(files, totalCounts);
            totalCounts = 0;
            result = personalPublishFilesService.getFilesByFollower(files, totalCounts);
        } catch (Exception e) {
            log.error("获取最新帖子异常", e);
            return ApiResult.newErrorResult(
                    PublishFilesErrorEnum.GET_FILES_FAIL.getErrorCode(),
                    PublishFilesErrorEnum.GET_FILES_FAIL.getErrorMessage());
        }
        return result;
    }


    @RequestMapping("getFilesById.htm")
    @ResponseBody
    public ApiResult<ArrayList<PersonalFeed>> getFilesById(
            PersonalPublishFiles files) {

        ApiResult<ArrayList<PersonalFeed>> result = new ApiResult<ArrayList<PersonalFeed>>();
        try {
            if (null == files) {
                return ApiResult.newErrorResult(
                        GenericErrorEnum.NOT_EMPTY.getErrorCode(), "参数"
                                + GenericErrorEnum.NOT_EMPTY.getErrorMessage());
            }
            if (files.getPublishFileId() == 0) {
                return ApiResult.newErrorResult(
                        GenericErrorEnum.NOT_EMPTY.getErrorCode(), "帖子id"
                                + GenericErrorEnum.NOT_EMPTY.getErrorMessage());
            }

            totalCounts = 1;
            result = personalPublishFilesService.getFilesById(files, totalCounts);
        } catch (Exception e) {
            log.error("获取最新帖子异常", e);
            return ApiResult.newErrorResult(
                    PublishFilesErrorEnum.GET_FILES_FAIL.getErrorCode(),
                    PublishFilesErrorEnum.GET_FILES_FAIL.getErrorMessage());
        }
        return result;
    }

    @RequestMapping("getFeedsBymemberId.htm")
    @ResponseBody
    public ApiResult<ArrayList<PersonalFeed>> getFeedsBymemberId(
            PersonalPublishFiles files, Integer totalCount) {

        ApiResult<ArrayList<PersonalFeed>> result = new ApiResult<ArrayList<PersonalFeed>>();
        try {
            if (null == files) {
                return ApiResult.newErrorResult(
                        GenericErrorEnum.NOT_EMPTY.getErrorCode(), "参数"
                                + GenericErrorEnum.NOT_EMPTY.getErrorMessage());
            }
            if (files.getMemberId() == 0) {
                return ApiResult.newErrorResult(
                        GenericErrorEnum.NOT_EMPTY.getErrorCode(), "会员id"
                                + GenericErrorEnum.NOT_EMPTY.getErrorMessage());
            }

            totalCounts = personalPublishFilesService.findCntMembers(files);
            initPages(files, totalCounts);
            result = personalPublishFilesService.getFilesBymemberId(files, totalCounts);
        } catch (Exception e) {
            log.error("获取最新帖子异常", e);
            return ApiResult.newErrorResult(
                    PublishFilesErrorEnum.GET_FILES_FAIL.getErrorCode(),
                    PublishFilesErrorEnum.GET_FILES_FAIL.getErrorMessage());
        }
        return result;
    }

    @RequestMapping("queryFeedStatus.htm")
    @ResponseBody
    public ApiResult<Integer> queryFeedStatus(
            PersonalPublishFiles files, Integer totalCount) {

        ApiResult<Integer> result = new ApiResult<Integer>();
        try {
            if (null == files) {
                return ApiResult.newErrorResult(
                        GenericErrorEnum.NOT_EMPTY.getErrorCode(), "参数"
                                + GenericErrorEnum.NOT_EMPTY.getErrorMessage());
            }
            if (files.getPublishFileId() == 0) {
                return ApiResult.newErrorResult(
                        GenericErrorEnum.NOT_EMPTY.getErrorCode(), "帖子id"
                                + GenericErrorEnum.NOT_EMPTY.getErrorMessage());
            }

            totalCounts = personalPublishFilesService.findCntMembers(files);
            initPages(files, totalCounts);
            result = personalPublishFilesService.queryFeedStatus(files, totalCounts);
        } catch (Exception e) {
            log.error("查询帖子状态异常", e);
            return ApiResult.newErrorResult(
                    PublishFilesErrorEnum.RESTORE_FILES_FAIL.getErrorCode(),
                    PublishFilesErrorEnum.RESTORE_FILES_FAIL.getErrorMessage());
        }
        return result;
    }

    @RequestMapping("getFeedIdByTags.htm")
    @ResponseBody
    public ApiResult<Map<Integer, PersonalPublishFiles>> getFeedIdByTags(
            PersonalPublishFiles files, Integer totalCount) {

        try {
            if (null == files) {
                return ApiResult.newErrorResult(
                        GenericErrorEnum.NOT_EMPTY.getErrorCode(), "参数"
                                + GenericErrorEnum.NOT_EMPTY.getErrorMessage());
            }

            
            totalCounts = personalPublishFilesService.findCntMembers(files);
            initPages(files, totalCounts);
            return personalPublishFilesService.getFeedIdByTags(files, totalCounts);
        } catch (Exception e) {
            log.error("查询帖子状态异常", e);
            return ApiResult.newErrorResult(
                    PublishFilesErrorEnum.RESTORE_FILES_FAIL.getErrorCode(),
                    PublishFilesErrorEnum.RESTORE_FILES_FAIL.getErrorMessage());
        }
    }

    @RequestMapping("getItemIdByFeedId.htm")
    @ResponseBody
    public ApiResult<ArrayList<Integer>> getItemIdByFeedId(
            PersonalPublishFiles files, Integer totalCount) {

        ApiResult<ArrayList<Integer>> result = new ApiResult<ArrayList<Integer>>();
        try {
            if (null == files) {
                return ApiResult.newErrorResult(
                        GenericErrorEnum.NOT_EMPTY.getErrorCode(), "参数"
                                + GenericErrorEnum.NOT_EMPTY.getErrorMessage());
            }

            totalCounts = personalPublishFilesService.findCntMembers(files);
            initPages(files, totalCounts);
            result = personalPublishFilesService.getItemIdByFeedId(files, totalCounts);
        } catch (Exception e) {
            log.error("查询帖子状态异常", e);
            return ApiResult.newErrorResult(
                    PublishFilesErrorEnum.RESTORE_FILES_FAIL.getErrorCode(),
                    PublishFilesErrorEnum.RESTORE_FILES_FAIL.getErrorMessage());
        }
        return result;
    }


}
